//
//  TTSDiscoverViewController.m
//  TTSVideo
//
//  Created by chenHR on 16/2/29.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TTSDiscoverViewController.h"
#import "TTSDiscoverHomeCollectionViewCell.h"
#import "TTSAppToolsAFN.h"
#import "TTSDiscoverModel.h"
#import "TTSDiscoverDetailsViewController.h"
@interface TTSDiscoverViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,retain)UICollectionView *findCollectionView;
@property (nonatomic,retain)NSMutableArray *discoverArray;
@end

@implementation TTSDiscoverViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 左侧菜单按钮
    UIImage *img = [UIImage imageNamed:@"iconfont-menu"];
    img = [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:img style:UIBarButtonItemStyleDone target:self action:@selector(menuBtnAction:)];
    
    // 标题
    UIImageView *titleImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"title"]];
    titleImgView.frame = CGRectMake(142, 4, 100, 35);
    [self.navigationController.navigationBar addSubview:titleImgView];
    
    // 搜索按钮
    UIImage *imgSearch = [UIImage imageNamed:@"iconfont-sousuo-4"];
    imgSearch = [imgSearch imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:imgSearch style:UIBarButtonItemStyleDone target:self action:@selector(searchBtnAction:)];
    
    self.discoverArray = [NSMutableArray array];
    [self getDateHomeDiscover];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(WIDTH / 2 - 2, WIDTH /2 - 2);
    flowLayout.minimumInteritemSpacing = 4;
    flowLayout.minimumLineSpacing = 4;
    
    self.findCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 36) collectionViewLayout:flowLayout];
    self.findCollectionView.backgroundColor = [UIColor whiteColor];
    self.findCollectionView.delegate = self;
    self.findCollectionView.dataSource = self;

    [self.findCollectionView registerClass:[TTSDiscoverHomeCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:self.findCollectionView];
    
    
}

// 解析homeDiscover 数据
- (void)getDateHomeDiscover{
    
    NSString *urlStr = @"http://baobab.wandoujia.com/api/v1/categories";
    [TTSAppToolsAFN getDataWithAFNurl:urlStr block:^(id result) {
        for (NSDictionary *dic in result) {
            TTSDiscoverModel *model = [[TTSDiscoverModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.discoverArray addObject:model];
            
        }
        [self.findCollectionView reloadData];
    } errorBlock:^(NSError *error) {
        
        
    }];
    
}



// 实现collection 协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.discoverArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TTSDiscoverHomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    TTSDiscoverModel *model = [self.discoverArray objectAtIndex:indexPath.row];
    cell.model = model;
    return  cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    TTSDiscoverDetailsViewController *discoverDetails = [[TTSDiscoverDetailsViewController alloc]init];
    TTSDiscoverModel *model = [self.discoverArray objectAtIndex:indexPath.row];
    discoverDetails.title = model.name;
    [self.navigationController pushViewController:discoverDetails animated:YES];
    
}

- (void)menuBtnAction:(UIButton *)btn{
    NSLog(@"菜单");
}

- (void)searchBtnAction:(UIBarButtonItem *)btn{
    NSLog(@"搜索");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
